home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2 Examples.sit / Raven 1.2 Examples / Quill / Source / ResourceNode.cpp < prev    next >
C/C++ Source or Header  |  1997-01-31  |  6KB  |  246 lines

  1. /*
  2.  *  File:       ResourceNode.cpp
  3.  *  Summary:       Abstract base class CResourceTable nodes.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <->     1/17/96    JDJ        Created
  12.  */
  13.  
  14. #include "ResourceNode.h"
  15.  
  16. #include <ZContextMenu.h>
  17. #include <ZHierarchicalTable.h>
  18. #include <ZIntConversions.h>
  19. #include <ZQDShapes.h>
  20. #include <ZQuickDrawUtils.h>
  21. #include <ZWindow.h>
  22.  
  23. #include "Constants.h"
  24. #include "ViewContainer.h"
  25.  
  26.  
  27. //-----------------------------------
  28. //    Constants
  29. //
  30. const short kIDWidth   = 50;
  31. const short kNameWidth = 160;
  32.  
  33.  
  34. // ===================================================================================
  35. //    class CResourceNode
  36. // ===================================================================================
  37.  
  38. //---------------------------------------------------------------
  39. //
  40. // CResourceNode::~CResourceNode
  41. //
  42. //---------------------------------------------------------------
  43. CResourceNode::~CResourceNode()
  44. {
  45.     CViewContainer* container = CViewContainer::GetContainer(mRsrcMap->GetType(), mID);
  46.     if (container != nil) {
  47.         TView* top = container->GetTopView();
  48.         TWindow* window = dynamic_cast<TWindow*>(top);
  49.         window->Close();
  50.     }
  51. }
  52.  
  53.  
  54. //---------------------------------------------------------------
  55. //
  56. // CResourceNode::CResourceNode (THierarchicalTable*, CResourceMap*)
  57. //
  58. //---------------------------------------------------------------
  59. CResourceNode::CResourceNode(THierarchicalTable* table, CResourceMap* rsrcMap) : TBaseTableNode(table, nil)
  60. {
  61.     ASSERT(rsrcMap != nil);
  62.  
  63.     mRsrcMap = rsrcMap;
  64.     mID = 0;
  65. }
  66.  
  67.  
  68. //---------------------------------------------------------------
  69. //
  70. // CResourceNode::CResourceNode (THierarchicalTable*, TSubNode*, CResourceMap*, ResID)
  71. //
  72. //---------------------------------------------------------------
  73. CResourceNode::CResourceNode(THierarchicalTable* table, TSubNode* parent, CResourceMap* rsrcMap, ResID id) : TBaseTableNode(table, parent)
  74. {
  75.     ASSERT(rsrcMap != nil);
  76.     ASSERT(id > 0);
  77.     
  78.     mRsrcMap = rsrcMap;
  79.     mID = id;
  80. }
  81.  
  82.  
  83. //---------------------------------------------------------------
  84. //
  85. // CResourceNode::GetSize
  86. //
  87. //---------------------------------------------------------------
  88. TLongSize CResourceNode::GetSize() const
  89. {
  90.     TLongSize size;
  91.     
  92.     size.width  = kIDWidth + kNameWidth;
  93.     size.height = mTextTraits.GetFontLineHeight();
  94.     
  95.     return size;
  96. }
  97.  
  98.  
  99. //---------------------------------------------------------------
  100. //
  101. // CResourceNode::UpdateResource
  102. //
  103. //---------------------------------------------------------------
  104. void CResourceNode::UpdateResource() const
  105. {
  106.     CViewContainer* container = CViewContainer::GetContainer(mRsrcMap->GetType(), mID);
  107.     if (container != nil) 
  108.         container->UpdateResource();
  109. }
  110.  
  111. #pragma mark ハ
  112.  
  113. //---------------------------------------------------------------
  114. //
  115. // CResourceNode::OnDraw
  116. //
  117. //---------------------------------------------------------------
  118. void CResourceNode::OnDraw(TCanvas& canvas, const TRect& cellBounds)
  119. {
  120.     TTextTrait traits = mTextTraits;
  121.     
  122.     if (mRsrcMap->IsDirty(mID)) {
  123.         STextTrait traitsRecord = mTextTraits.GetTraits();
  124.         traitsRecord.style = underline;
  125.         
  126.         traits = traitsRecord;
  127.     }
  128.     
  129.     TRect bounds = cellBounds;
  130.     bounds.right = bounds.left + kIDWidth;
  131.     
  132.     string text = ShortToStr(mID);
  133.     text = TruncateText(text, bounds.GetWidth());            // ・・・ハshould take into account text traits...
  134.     TTextShape::Draw(canvas, text, bounds, traits);
  135.     
  136.     bounds.left = bounds.right;
  137.     bounds.right = cellBounds.right;
  138.     string name = mRsrcMap->GetResourceName(mID);
  139.     text = TruncateText(name, bounds.GetWidth(), truncEnd);    
  140.     TTextShape::Draw(canvas, text, bounds, traits);
  141. }
  142.  
  143.  
  144. //---------------------------------------------------------------
  145. //
  146. // CResourceNode::OnMouseDown
  147. //
  148. //---------------------------------------------------------------
  149. bool CResourceNode::OnMouseDown(const TMouseEvent& event)
  150. {
  151.     if (event.GetClickCount() == 2)
  152.         this->EditResource();
  153.         
  154.     return kHandled;
  155. }
  156.  
  157.  
  158. //---------------------------------------------------------------
  159. //
  160. // CResourceNode::OnContextMenu
  161. //
  162. //---------------------------------------------------------------
  163. bool CResourceNode::OnContextMenu(const TMouseEvent& event)
  164. {
  165.     if (!this->IsSelected()) {
  166.         mTable->SelectOneNode(this);
  167.         mTable->HandleUpdate();
  168.     }
  169.  
  170.     TContextMenu* popup = new TContextMenu(mTable, event.GetGlobalPosition(), 300);
  171.     popup->Post();
  172.  
  173.     return kHandled;
  174. }
  175.  
  176.  
  177. //---------------------------------------------------------------
  178. //
  179. // CResourceNode::OnKeyDown
  180. //
  181. //---------------------------------------------------------------
  182. bool CResourceNode::OnKeyDown(const TKeyEvent& event)
  183. {
  184.     bool handled = true;
  185.     
  186.     switch (event.GetChar()) {
  187.         case kReturnChar:
  188.         case kEnterChar:
  189.             if (this->IsSelected())
  190.                 this->EditResource();
  191.             break;
  192.             
  193.         default:
  194.             handled = false;
  195.     }
  196.     
  197.     if (!handled) 
  198.         handled = Inherited::OnKeyDown(event);
  199.     
  200.     return handled;
  201. }
  202.  
  203.  
  204.  
  205. //---------------------------------------------------------------
  206. //
  207. // CResourceNode::OnMenuCommand
  208. //
  209. //---------------------------------------------------------------
  210. bool CResourceNode::OnMenuCommand(const MenuCommand& command)
  211. {
  212.     bool handled = true;
  213.     
  214.     if (command == kEditCmd) {                // from context menu
  215.         if (this->IsSelected())
  216.             this->EditResource();
  217.         
  218.     } else
  219.         handled = Inherited::OnMenuCommand(command);
  220.         
  221.     return handled;
  222. }
  223.  
  224.         
  225. //---------------------------------------------------------------
  226. //
  227. // CResourceNode::OnCommandStatus
  228. //
  229. //---------------------------------------------------------------
  230. bool CResourceNode::OnCommandStatus(const MenuCommand& command, SCommandStatus& status)
  231. {
  232.     bool handled = true;
  233.     
  234.     if (command == kEditCmd)
  235.         status.enabled = true;
  236.         
  237.     else
  238.         handled = Inherited::OnCommandStatus(command, status);
  239.         
  240.     return handled;
  241. }
  242.  
  243.  
  244.  
  245.  
  246.